﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AirlineCommon;
using AirlineServiceFacade;
namespace AirlineSystemApp
{
    public partial class frmReservation : Form
    {
        private IAirlineServices airlineSv;
        private List<Flight> lstFlight;
        AirlineFactory factory;
        TravelClass travelclass;
        PassengerCommon passenger;
        Sector sector;
        private DateTime dtravelDate;
        public frmReservation()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadSector();
            
        }

        private void LoadSector()
        {
            try
            {
                airlineSv = new AirlineServices();
                string stFrom = cmbFrom.SelectedItem.ToString();
                string stTo = cmbTo.SelectedItem.ToString();
                sector = airlineSv.GetSectorDetail(stFrom + " - " + stTo);
                lblDay1.Text = sector.WeekDay1;
                lblDay2.Text = sector.WeekDay2;
                LoadFlightList(sector);
                grbFlights.Enabled = true;
                dtpTravelDate.Focus();
            }
            catch 
            {
                MessageBox.Show("No Sector exists, Please select again.");
                grbFlights.Enabled = false;
            }
        }

        private void LoadFlightList(Sector st)
        {
            try
            {
                if (airlineSv != null)
                {
                    lstFlight=airlineSv.GetFlightList(st);
                    lstFlightNo.DisplayMember = "FlightNo";
                    lstFlightNo.ValueMember = "FlightNo";
                    lstFlightNo.DataSource = lstFlight;
                }
            }
            catch
            {
                MessageBox.Show("Error in loading flight");
            }
        }

        private void lstFlightNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(lstFlight!=null )
            {
                if (lstFlight.Count > 0)
                {
                    lblTravelDate.Text = lstFlight.Single(s => s.FlightNo == lstFlightNo.SelectedValue.ToString()).FlightDate.ToShortDateString();
                    lblDepTime.Text = lstFlight.Single(s => s.FlightNo == lstFlightNo.SelectedValue.ToString()).DepTime;
                    lblArrTime.Text = lstFlight.Single(s => s.FlightNo == lstFlightNo.SelectedValue.ToString()).ArrTime;
                    dtravelDate = lstFlight.Single(s => s.FlightNo == lstFlightNo.SelectedValue.ToString()).FlightDate;
                }
            }
        }

        private void cmdCheckAvailability_Click(object sender, EventArgs e)
        {
            if (cmbFrom.SelectedItem == null || cmbTo.SelectedItem == null)
            {
                MessageBox.Show("Please Select Flight Sector", "Airline System");
                return;
            }
            if (cmbClass.SelectedItem != null)
            {
                if (cmbClass.SelectedItem.ToString().ToLower() == "first")
                {
                    factory = new FirstFactory();
                   
                }
                else if (cmbClass.SelectedItem.ToString().ToLower() == "business")
                {
                    factory = new BusinessFactory ();

                }
                else
                {
                    factory = new EconomyFactory ();
                }
                if (airlineSv != null)
                {
                    travelclass = airlineSv.CreateTravelClass(factory);
                    passenger = airlineSv.CreatePassenger(factory);
                    int seatAvailable=travelclass.GetSeatAvailable(lstFlightNo.SelectedValue.ToString(), dtpTravelDate.Value);
                    if (seatAvailable > 0)
                    {
                        travelclass.SectorID = sector.SectorID;
                        txtFareAmt.Text = travelclass.GetFareAmount().ToString();
                        grbPassengers.Enabled = true;
                        passenger.Status = "Confirmed";
                        //if ((factory is FirstFactory) || (factory is BusinessFactory))
                        //    travelclass.Status = "Confirmed";
                        cmdSave.Enabled = true;
                    }
                    else
                    {
                       
                        if ((factory is FirstFactory) || (factory is BusinessFactory))
                        {
                            MessageBox.Show("Flight Date invalid or Ticket Class empty, Pls select Again!");
                            txtFareAmt.Text = "0";
                            grbPassengers.Enabled = false;
                            cmdSave.Enabled = false;
                        }
                        else if (factory is EconomyFactory)
                        {
                            travelclass.SectorID = sector.SectorID;
                            passenger.Status = "Overbook";
                            txtFareAmt.Text = travelclass.GetFareAmount().ToString();
                            grbPassengers.Enabled = true;
                            cmdSave.Enabled = true;
                        }

                    }
                }
            }
        }

        private void frmReservation_Load(object sender, EventArgs e)
        {
            grbPassengers.Enabled = false;
            grbFlights.Enabled = false;
            cmdSave.Enabled = false;
            tsMessage.Text = "This Form use Asbstract Factory and Facade Pattern";
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            passenger.PNRNo = airlineSv.GeneratePNRNo();
            passenger.FlightNo = lstFlightNo.SelectedValue.ToString();
            passenger.TravelDate = dtpTravelDate.Value;
            passenger.FName = txtFName.Text;
            passenger.LName = txtLName.Text;
            passenger.Age = int.Parse(txtAge.Text);
            if (rbMale.Checked)
                passenger.Gender = "M";
            else if (rbFemale.Checked)
                passenger.Gender = "F";
            passenger.Class = cmbClass.SelectedItem.ToString();
            if (rbVeg.Checked)
                passenger.MealPref = "Veg.";
            else if(rbNonveg.Checked )
                passenger.MealPref = "Nonveg";
            if (factory is FirstFactory)
            {
                if(rbAisle.Checked )
                    ((FirstPassenger)passenger).SeatPref = "Aisle";
                else if(rbWindow.Checked )
                    ((FirstPassenger)passenger).SeatPref = "Window";
                ((FirstPassenger)passenger).SSR = txtSSR.Text;
            }
            else if (factory is BusinessFactory)
            {
                if (rbAisle.Checked)
                    ((BusinessPassenger)passenger).SeatPref = "Aisle";
                else if (rbWindow.Checked)
                    ((BusinessPassenger)passenger).SeatPref = "Window";
            }//Set Flight
            Flight flight = new Flight();
            flight.FlightDate = dtpTravelDate.Value;
            flight.FlightNo = lstFlightNo.SelectedValue.ToString();
            //Set Ticket
            Ticket ticket = new Ticket();
            ticket.PNRNo = passenger.PNRNo;
            ticket.TicketNo = passenger.PNRNo;
            ticket.TranDate = DateTime.Now;
            ticket.TranType = "Collection";
            ticket.Amount = float.Parse(txtFareAmt.Text);
            try
            {
                if (airlineSv != null)
                {
                    if (airlineSv.ReserTicket(passenger, ticket, travelclass, flight))
                    {
                        MessageBox.Show("Save reservation ticket success, Pls keep PNR Number:" + ticket.PNRNo);
                        PrintTickets();
                    }
                    else
                        MessageBox.Show("Save reservation ticket fail");
                }
            }
            catch
            {
                MessageBox.Show("Save reservation ticket fail");
            }
        }

        private void PrintTickets()
        {
            PrintTicket frmPrintTicket=new PrintTicket ();
            frmPrintTicket.strPNRNo = passenger.PNRNo;
                frmPrintTicket.strName = txtFName.Text.Trim () + " " + txtLName.Text.Trim ();
                frmPrintTicket.strFlightNo = lstFlightNo.SelectedValue.ToString();
                frmPrintTicket.strDepTime = lblDepTime.Text;
                frmPrintTicket.strArrTime = lblArrTime.Text;
                frmPrintTicket.strClass = cmbClass.Text;
                frmPrintTicket.strDate = dtpTravelDate.Value.Month.ToString () + "/" + dtpTravelDate.Value.Day.ToString () + "/" + dtpTravelDate.Value.Year.ToString ();
                frmPrintTicket.Status = passenger .Status ;
                frmPrintTicket.ShowDialog();
        }

        private void dtpTravelDate_Validating(object sender, CancelEventArgs e)
        {
            if (dtpTravelDate.Value.Date  < DateTime.Now.Date )
            {
                MessageBox.Show("Flight Date can not less than Today");
                e.Cancel = true;
            }
            else if (dtravelDate.Date != dtpTravelDate.Value.Date)
            {
                //frmScheduledFlight frmSch = new frmScheduledFlight();
                //frmSch.ShowDialog();
                MessageBox.Show("Flight Date invalid, Flight Date is "+dtravelDate.Date.ToShortDateString ()+", Please select again. ");
                e.Cancel = true;
            }
        }

        private void cmbClass_Validating(object sender, CancelEventArgs e)
        {
            if (cmbClass.SelectedItem == null)
            {
                MessageBox.Show("Class Seat is not empty");
                e.Cancel = true;
            }
           
        }

        private void txtAge_Validating(object sender, CancelEventArgs e)
        {
            if (!(IsNumeric(txtAge.Text, System.Globalization.NumberStyles.Integer)))
            {
                MessageBox.Show("Age invalid format, it is number (1--100)", "Airline System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
            }
        }
        public bool IsNumeric(string Val, System.Globalization.NumberStyles NumberStyle)
        {
            int result;

            return int.TryParse(Val, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
        }
    }
}
